package com.nhjf.common;



import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;

import com.nhjf.common.util.tools.LogMessage;

public class DataConvertor {

	public static Integer parseInteger(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Integer.valueOf(str);
	}
	
	public static int parseInt(String str) {
		if (str == null || str.trim().equals(""))
			return 0;
		return Integer.parseInt(str);
	}
	
	public static int parseInt(Integer str) {
		if (str == null)
			return 0;
		return str.intValue();
	}

	public static Double parseDouble(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Double.valueOf(str);
	}

	public static double parseDoubleValue(String str) {
		if (str == null || str.trim().equals(""))
			return 0;
		return Double.valueOf(str).doubleValue();
	}

	public static Date parseDate(String str, String format) {
		if (str == null || str.trim().equals(""))
			return null;
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			Date date = simpleDateFormat.parse(str);
			return date;
		} catch (Exception e) {
			return null;
		}
	}

	public static Timestamp parseTimestamp(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		return Timestamp.valueOf(str);
	}

	/**
	 * 把yyyy-MM-dd格式的字符串转换成Date
	 * 
	 * @param dateStr
	 * @return Timestamp
	 */
	public final static Date str2Date(String dateStr) {
		if (dateStr == null || dateStr.length() == 0) {
			return null;
		}
		
		DateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
		Date da = null;
		try {
			da = df.parse(dateStr);
		} catch (Exception e) {
			LogMessage.logwrite(DataConvertor.class, e.getMessage());
			return null;
		}
		return da;
	}
	
	public static String getCurDateStr(String formatStr){
		Date curDate=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
		return sdf.format(curDate);
	}
	public final static String date2Str(Date da, String pattern) {
		if (da == null) {
			return "";
		}
		SimpleDateFormat df = new java.text.SimpleDateFormat(pattern);
		return df.format(da);
	}
}
